package cn.mandifal.cart.controller;
import java.util.List;

import cn.mandifal.order.service.OrderService;
import cn.mandifal.pojo.DpOrders;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.dubbo.config.annotation.Reference;

import entity.PageResult;
import entity.Result;
/**
 * 订单 控制层
 * @author shun
 *
 */
@RestController
@RequestMapping("/orderController")
public class OrderController {

	@Reference
	private OrderService orderService;
	

	/**
	 * 增加
	 * @param order 总订单实体
	 * @return
	 */
	@RequestMapping("/add")
	public Result add(@RequestBody DpOrders order){
		
		//获取当前登录人账号
		String username = SecurityContextHolder.getContext().getAuthentication().getName();
		//判断商品的库存和状态是否合法
		Boolean aBoolean = orderService.checkStockAndStatusAtAddOrder(username);
		if (! aBoolean){
			return  Result.failed("存在已下架或库存不足的商品");
		}
		order.setUserId(username);
		//订单来源  PC
		order.setSourceType("2");
		
		try {
			orderService.add(order);
			return Result.success( "增加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return  Result.failed( "增加失败");
		}
	}
	
	/**
	 * 修改
	 * @param order
	 * @return
	 */
	@RequestMapping("/update")
	public Result update(@RequestBody DpOrders order){
		try {
			orderService.update(order);
			return Result.success("修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failed( "修改失败");
		}
	}	
	
	/**
	 * 获取实体
	 * @param id
	 * @return
	 */
	@RequestMapping("/findOne")
	public DpOrders findOne(String id){
		return orderService.findOne(id);		
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@RequestMapping("/delete")
	public Result delete(String [] ids){
		try {
			orderService.delete(ids);
			return Result.success( "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return Result.failed("删除失败");
		}
	}
	
}
